using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Deepcode.Flux.Core.Data;

namespace Deepcode.Flux.Core.Systems.Workflow.Bases
{
	/// <summary>
	/// Represents a task in the workflow system
	/// </summary>
	public class Task
	{
		// Private fields for storing record fields //
		private int			_PK_ID = -1;
		private string		_HandlerCodeID = "";
		private int			_FK_DefinitionID = -1;
		private int			_FK_ProcessID = -1;
		private int			_FK_TaskingID = -1;
		private int			_FK_ForOperatorID = -1;
		private int			_FK_ForOperatorGroupID = -1;
		private bool		_ForSystem = false;
		private DateTime	_ForSystemExecDT = System.DateTime.Now;
		private DateTime	_StartDT = System.DateTime.Now;
		private DateTime	_DueDT = System.DateTime.Now.AddYears(1000);
		private string		_TaskCaption = "";
		private string		_DataItemText = "";
		private int			_FK_DataID = -1;
		
		
		// Public fields exposing record fields //
		public int PK_ID{ get{ return _PK_ID; } set{ _PK_ID=value; }}
		public string HandlerCodeID{ get{ return _HandlerCodeID; } set{ _HandlerCodeID=value; }}
		public int FK_DefinitionID{ get{ return _FK_DefinitionID; } set{ _FK_DefinitionID=value; }}
		public int FK_ProcessID{ get{ return _FK_ProcessID; } set{ _FK_ProcessID=value; }}
		public int FK_TaskingID{ get{ return _FK_TaskingID; } set{ _FK_TaskingID=value; }}
		public int FK_ForOperatorID{ get{ return _FK_ForOperatorID; } set{ _FK_ForOperatorID=value; }}
		public int FK_ForOperatorGroupID{ get{ return _FK_ForOperatorGroupID; } set{ _FK_ForOperatorGroupID=value; }}
		public bool ForSystem{ get{ return _ForSystem; } set{ _ForSystem=value; }}
		public DateTime ForSystemExecDT{ get{ return _ForSystemExecDT; } set{ _ForSystemExecDT=value; }}
		public DateTime StartDT{ get{ return _StartDT; } set{ _StartDT = value; }}
		public DateTime DueDT{ get{ return _DueDT; } set{ _DueDT = value; }}
		public string TaskCaption{ get{ return _TaskCaption; } set{ _TaskCaption=value; }}
		public string DataItemText{ get{ return _DataItemText; } set{ _DataItemText=value; }}
		public int FK_DataID{ get{ return _FK_DataID; } set{ _FK_DataID=value; }}

		// Calced fields
		public string CalcDue
		{
			get
			{
				string OKFormat = "<font color=green>{0}</font>";
				string LATEFormat = "<font color=red>{0}</font>";
				string useFormat = OKFormat;
				
				if (DueDT > System.DateTime.Now.AddYears(100)) return String.Format(OKFormat, "No due date");
				if (DueDT <= System.DateTime.Now) useFormat = LATEFormat;
				return String.Format(useFormat, DueDT.ToString("dd-MM-yy HH:mm"));
			}
		}

		// Link backs
		public Process ParentProcess { get { return new Processes().GetProcessByID(this.FK_ProcessID); } }
		public Definition ParentDefinition { get { return new Definitions().GetByID(this.FK_DefinitionID); } }
		public Tasking ParentTasking { get { return new Taskings().GetTaskingByID(this.FK_TaskingID); } }
		public TaskExitPath[] ExitPaths
		{
			get
			{
				TaskExitPaths teps = new TaskExitPaths();
				return teps.GetExitPathsForTaskingID( this.FK_TaskingID );
			}
		}

		// Constructor to create new without loading
		public Task() { }

		// Constructor loading from a data row
		public Task(DataRow InitFromRow)
		{
			_PK_ID = SQLUtility.DBNull(InitFromRow["PK_ID"], -1);
			_HandlerCodeID = SQLUtility.DBNull(InitFromRow["HandlerCodeID"], "");
			_FK_DefinitionID = SQLUtility.DBNull(InitFromRow["FK_DefinitionID"], -1);
			_FK_ProcessID = SQLUtility.DBNull(InitFromRow["FK_ProcessID"], -1);
			_FK_TaskingID = SQLUtility.DBNull(InitFromRow["FK_TaskingID"], -1);
			_FK_ForOperatorID = SQLUtility.DBNull(InitFromRow["FK_ForOperatorID"], -1);
			_FK_ForOperatorGroupID = SQLUtility.DBNull(InitFromRow["FK_ForOperatorGroupID"], -1);
			_ForSystem = SQLUtility.DBNull(InitFromRow["ForSystem"], false);
			_ForSystemExecDT = SQLUtility.DBNull(InitFromRow["ForSystemExecDT"], System.DateTime.Now);
			_TaskCaption = SQLUtility.DBNull(InitFromRow["TaskCaption"], "");
			_DataItemText = SQLUtility.DBNull(InitFromRow["DataItemText"], "");
			_FK_DataID = SQLUtility.DBNull(InitFromRow["FK_DataID"], -1);
			_StartDT = SQLUtility.DBNull(InitFromRow["StartDT"], System.DateTime.Now);
			_DueDT = SQLUtility.DBNull(InitFromRow["DueDT"], System.DateTime.Now.AddYears(1000));
		}
	}
}
